package com.md.notes;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.md.notes.model.module.permission.PermissionModel;
import com.md.notes.util.VersionUtils;

public class LoginActivity extends AppCompatActivity {
    private AppCompatActivity activity;
    private Button btnLogin;
    private EditText etUsername;
    private EditText etPassword;
    private CheckBox cbRemember;
    private PermissionModel permissionModel;
    private TextView versionText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();
        initData();
        addListener();
    }

    private void initView() {
        activity = LoginActivity.this;
        btnLogin = findViewById(R.id.btn_login);
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        cbRemember = findViewById(R.id.cb_remember);
        versionText = findViewById(R.id.versionText);
    }

    private void initData() {
        // 从SharedPreferences中获取保存的登录信息
        SharedPreferences sp = getSharedPreferences("login_info", MODE_PRIVATE);
        String savedUsername = sp.getString("username", "");
        String savedPassword = sp.getString("password", "");

        // 如果有保存的用户名和密码，则显示在输入框中
        if (!TextUtils.isEmpty(savedUsername) && !TextUtils.isEmpty(savedPassword)) {
            etUsername.setText(savedUsername);
            etPassword.setText(savedPassword);
            cbRemember.setChecked(true);  // 自动勾选"记住密码"选项
        }

        if (null == permissionModel){
            permissionModel = new PermissionModel();
        }
        permissionModel.init(activity, () -> {
            // 获得权限后保存文件
        });

        versionText.setText("V"+VersionUtils.getAppVersionName(activity));
    }

    private void addListener() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();

                // 输入验证
                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(activity, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(activity, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (permissionModel.checkAndRequestPermissions(activity)) {
                    // 执行登录
                    doLogin(username, password);
                }
            }
        });
    }

    private void doLogin(String username, String password) {
        // TODO: 实现具体的登录逻辑
        // 这里可以调用网络请求进行登录验证
        // 示例代码：
        if ("admin".equals(username) && "123456".equals(password)) {
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
            // 如果记住密码被选中，保存用户名密码
            if (cbRemember.isChecked()) {
                saveLoginInfo(username, password);
            }
            // 登录成功后跳转到主页面
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        } else {
            Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }

    private void saveLoginInfo(String username, String password) {
        SharedPreferences sp = getSharedPreferences("login_info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.apply();
    }
}